07. Avoiding Unnecessary Reloads

Avoiding Unnecessary Reloads

Question:

Right now our CursorLoader has a little bug, which is that it doesn’t automatically update when the underlying SQLite database changes.

Why was the list updating before and not now? Let’s go back in time and look at the old code.

The displayDatabaseInfo was previously called both in onStart() and right after the menu item was pressed.
This made absolutely sure the data was up to date, but it was also loading data on the main thread a bunch of times unnecessarily. Loading on the main thread slows down the app, as previously discussed, in addition, doing it uselessly is just bad.

When was it uselessly loading data? Well onStart is triggered whenever the application is rotated, or if you navigate away for a second and then back. If you rotate the app or navigate away and back from the application - do you really need to get the data from the database again? Has anything in the database changed? The answer is no, nothing in the database has changed, so you should not need to keep reloading the data.

This is actually one of the main problems a CursorLoader is meant to resolve - we want to keep track of whether the data has been loaded and if so, avoid reloading the data unless we need to.

So it is clear we do not need to reload the method every time onStart is called. When do we need to reload the data?

Start Quiz:

Solution:

We only need to update the cursor when some of the data in the sqlite database changes.

The user closing and re-opening the app, the user rotating the app, and scrolling down in the list view will not actually change the data in the database, so we don't need to reload the data then.